package com.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.exception.MallException;
import com.mall.exception.MallExceptionEnum;
import com.mall.mapper.CategoryMapper;
import com.mall.model.po.Category;
import com.mall.model.vo.CategoryVo;
import com.mall.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {
   @Autowired
   private CategoryMapper categoryMapper;
    @Override
    public void addCategory(Category category) {
//        //添加的目录不存在
        Category oldCategory =categoryMapper.selectByName(category.getName());
        if (oldCategory!=null){
            throw new MallException(MallExceptionEnum.NAME_EXISTS);
        }
        int count =categoryMapper.insertSelective(category);
        //添加失败的处理
        if (count==0){
            throw new MallException(MallExceptionEnum.INSERT_FAILED);
        }
    }

    @Override
    public void updateCategory(Category category) {
//        //更改的目录不存在

        if(category.getName()!=null){
            //修改目录不能重名
            Category oldCategory = categoryMapper.selectByName(category.getName());
            if(oldCategory!=null && category.getName().equals(oldCategory.getName()) && !(category.getId().equals(oldCategory.getId()))){
                throw  new MallException(MallExceptionEnum.NAME_EXISTS);
            }
        int count =categoryMapper.updateByPrimaryKeySelective(category);
        //更改失败的处理
        if (count==0){
            throw new MallException(MallExceptionEnum.UPDATE_FAILED);
        }
    }
}

    @Override
    public void deleteCategory(Integer id) {
        Category category =categoryMapper.selectByPrimaryKey(id);
        if (category==null){
            throw new MallException(MallExceptionEnum.NOT_EXIST_CATEGORY);
        }
        int count=categoryMapper.deleteByPrimaryKey(id);
        if (count==0){
            throw new MallException(MallExceptionEnum.DELETE_FAILED);
        }
    }

    @Override
    public PageInfo<Category> listCategoryForAdmin(@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize) {
       //设置分页
        PageHelper.startPage(pageNum,pageSize,"type,order_num");//两个条件
//        查询所有数据
        List<Category> categoryPageInfo =categoryMapper.selectByExample(null);
//        组装分页对象
        PageInfo<Category> pageInfo =new PageInfo<Category>(categoryPageInfo);

        return pageInfo;

    }

    @Override
    public List<CategoryVo> listCategoryForCustomer() {
//        先查询所有数据
        List<Category> categories=categoryMapper.selectByExample(null);
//        将数据转换成vo对象
        List<CategoryVo> categoryVoList=new ArrayList<CategoryVo>();
        for (Category category:categories
             ) {
            CategoryVo categoryVo =new CategoryVo();
//            将数组对象转换成vo对象
            BeanUtils.copyProperties(category,categoryVo);
            categoryVoList.add(categoryVo);

        }
//        采用jdk8lamda表达式获取集合流来做，集合流采用cpu计算，性能最佳
//        将volist的数据转换为树形菜单数据
        categoryVoList=getTree(categoryVoList);
        return categoryVoList;
    }
//将数据转换为tree类型
    private static List<CategoryVo> getTree(List<CategoryVo> categoryVoList) {
//        将集合数据流入cpu进行遍历计算，过滤条件是父id是0就保留，否则不保留
//    collect 收集的意思，此处表示终止操作，集合流必须要有终止操作中间才会执行
    List<CategoryVo> tree =categoryVoList.stream().filter(categoryVo -> {
//        判断是否是一级目录即判断是否返回布尔值
        if (categoryVo.getParentId()==0){
            return true;
        }else{
            return false;
        }
    })
//            递归获取二级和三级目录
            .map(categoryVo -> {categoryVo.setChildCategory(getChildrenByParentId(categoryVo.getId(),categoryVoList));
            return categoryVo;
            }).collect(Collectors.toList());
    return tree;
    }

    private static List<CategoryVo> getChildrenByParentId(Integer parentId, List<CategoryVo> list) {
        List<CategoryVo> childCategorys =  list.stream().filter(categoryVo -> {
            if(categoryVo.getParentId()==parentId){
                return true;
            }else{
                return false;
            }
        }).map(categoryVo ->{
            categoryVo.setChildCategory(getChildrenByParentId(categoryVo.getId(),list));
            return categoryVo;
        }).collect(Collectors.toList());
        return childCategorys;
    }


}
